package com.asia.algorithmcode.dynamicProgram;


import java.util.Arrays;
import java.util.List;

/**
 * 139、单词拆分
 */
public class WordBreak {

    public static void main(String[] args) {
        String s = "applepenapple";
        List<String> dict = Arrays.asList("apple", "pen");

        System.out.println(new WordBreak().wordBreak(s, dict));

    }


    public boolean wordBreak(String s, List<String> wordDict) {
        int n = s.length();

        boolean[] dp = new boolean[n + 1];

        dp[0] = true;

        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < i; j++) {
                if (dp[j] && wordDict.contains(s.substring(j, i))) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[n];
    }


}
